<?php

namespace common\models\app;

use Yii;

/**
 * This is the model class for table "{{%app_device}}".
 *
 * @property int $id
 * @property int $os_id 版本序列号
 * @property string $sn 序列号
 * @property int $build build版本序列号
 * @property string|null $os 操作系统
 * @property string|null $model 设备机型
 * @property string|null $imei IMEI机器码
 * @property string|null $push_token 推送Token
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class AppDevice extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const OS_ANDROID = 'android';
    const OS_IOS = 'ios';
    const OS_WINDOWS = 'windows';
    //
    const OS_ID_ANDROID = 1;
    const OS_ID_IOS = 2;
    const OS_ID_H5 = 4;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%app_device}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['os_id', 'build', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['sn'], 'required'],
            [['sn', 'imei', 'model'], 'string', 'max' => 64],
            [['os'], 'string'],
            [['push_token'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'os_id' => '操作系统ID',
            'sn' => '序列号',
            'build' => 'build版本序列号',
            'os' => '操作系统',
            'model' => '设备机型',
            'imei' => 'IMEI机器码',
            'push_token' => '推送Token',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public static function getOsId($os) {
        if ($os == static::OS_ANDROID) {
            return static::OS_ID_ANDROID;
        }
        if ($os == static::OS_IOS) {
            return static::OS_ID_IOS;
        }
        if ($os == static::OS_WINDOWS) {
            return static::OS_ID_H5;
        }
        return 0;
    }

}
